package itcast.dao;

import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import itcast.JDBCutils.NewJDBCUtils;
import itcast.domain.User;
import itcast.domain.CarBean;
import itcast.domain.ShopsBean;

public class ShopsDaoimpl implements shopsDao {

	private JdbcTemplate template = new JdbcTemplate(NewJDBCUtils.getDataSource());

	/*
	 * 查询所有商品
	 * */
	@Override
	public List<ShopsBean> findallshops() {
		  String sql = "select * from shops";
	        List<ShopsBean> list = template.query(sql, new BeanPropertyRowMapper<ShopsBean>(ShopsBean.class));
//	        for (User user : list) {
//	            System.out.println(user);
//	        }  
	        return list;
	}

	/*
	 * 查询所有商品总数
	 * */
	@Override
	public int findshopTotalCount() {
	     String sql = "select count(*) from shops ";
	     return template.queryForObject(sql,Integer.class);
	}

	/*
	 * 后台实现的商品翻页传入开始页码start，和每一页显示rows！
	 * 
	 * */
	@Override
	public List<ShopsBean> findshopByPage(int start, int rows) {
		 String sql = "select * from shops limit ? , ? ";
		 return template.query(sql, new BeanPropertyRowMapper<ShopsBean>(ShopsBean.class),start,rows);
	}

	/*
	 * 添加商品进入购物车实现，把其的用户id与商品id
	 * 
	 * */
	@Override//可优化直接传入sBean对象
	public void addshop(int shopid, int uid,ShopsBean sBean) {
	    String sql="insert into carshopss values(?,?,?,?,?,?)";
	    
		template.update(sql,uid,shopid,sBean.getShopname(),sBean.getPrice(),sBean.getUrl(),1);
	}

	/*
	 * 根据商品id查询商品对象
	 * 
	 * */
	@Override
	public ShopsBean findshopid(int shopid) {
		String sql="select * from shops where shopid = ?";
		ShopsBean ShopsBean=template.queryForObject(sql, new BeanPropertyRowMapper<ShopsBean>(ShopsBean.class),shopid);
		return ShopsBean;
	}
	/*
	 * 根域用户id查询购物车，把该用户的购物车里的对象全部取出
	 * */
	@Override
	public List<CarBean> findcarshop(int userid) {
		String sql="select * from carshopss where userid = ?";
		List<CarBean> listcarBeans=template.query(sql, new BeanPropertyRowMapper<CarBean>(CarBean.class),userid);
		 return listcarBeans;
	}
	/*
	 * 原商品已经纯在，改其数量皆可
	 * */
	@Override
	public void updatecarshop(int shopid, int uid, int toal) {
		  String sql="UPDATE carshopss SET counts=? WHERE userid=? AND shopid=?;";
		  template.update(sql,toal,uid,shopid);
		
	}


	/*
	 *删除购物车里面商品 
	 * */
	@Override
	public void delcarshop(int shopid) {
		String sql="delete from carshopss where shopid= ?";
		  template.update(sql,shopid);
	}
/*
 * 修改商品数量
 * 
 * */
	@Override
	public void updateshopcount(int shopid, int shopcount) {
		  String sql="UPDATE carshopss SET counts=? WHERE shopid=?;";
		  template.update(sql,shopcount,shopid);
	}
/*
 * 要求用户id与商品id一样才取出
 * 
 * */
	@Override
	public CarBean findcarshops(String shopid, int i) {
	try {
		int _shopid=Integer.parseInt(shopid);
		
		String sql="select * from carshopss where userid = ? and shopid= ? ";
		CarBean carBean=template.queryForObject(sql, new BeanPropertyRowMapper<CarBean>(CarBean.class),i,_shopid);
		 return carBean;
	   } catch (Exception e) {
		   e.printStackTrace();	
	     return null;
	   }
		
		 
	}
}
